import java.util.ArrayList;
import java.util.Scanner;

public class MC0206小码哥的英语 {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        // code here
        int n = input.nextInt();
        input.nextLine();
        for (int i = 0; i < n; i++) {
            String s = input.nextLine();
            if (s.length() <= 1) {
                continue;
            }
            int ans1 = 0, ans2 = 0;
            // 计算变成模式一 (ABABAB...)
            for (int j = 0; j < s.length(); j++) {
                char targetChar = (j % 2 == 0) ? 'A' : 'B';
                if (s.charAt(j) == targetChar) {
                    ans1++;
                }
            }
            // 计算变成模式一 (BABABA...)
            for (int j = 0; j < s.length(); j++) {
                char targetChar = (j % 2 == 0) ? 'B' : 'A';
                if (s.charAt(j) == targetChar) {
                    ans2++;
                }
            }
            System.out.println(Math.min(ans1, ans2));
        }
        input.close();
    }
}
